// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Data;

namespace Northwind.DomainModel.EnterpriseLibrary
{

    /// <summary />
    partial class ShipperMapper
    {

        /// <summary />
        static ShipperMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Shipper> _FindAll(DataMapperCollection dataMappers)
        {
            DbCommand[] commands=new DbCommand[1];

            commands[0]=Database.GetSqlStringCommand(Resources.Sql.ShipperSelectAll);

            return Load(commands, dataMappers);
        }

        private Shipper.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Shipper.DataTransfer data=new Shipper.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Shipper.DataTransfer FillData(Shipper.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("ShipperID");
            if (!record.IsDBNull(ord))
                data.ShipperID=record.GetInt32(ord);

            ord=record.GetOrdinal("CompanyName");
            if (!record.IsDBNull(ord))
                data.CompanyName=record.GetString(ord);

            ord=record.GetOrdinal("Phone");
            if (!record.IsDBNull(ord))
                data.Phone=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static DbCommand _CreateSelectCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Shipper.PrimaryKey key)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.ShipperSelect,
                    parameterBuilder.GetMarker("ShipperID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipperID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                key.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateDeleteCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Shipper.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.ShipperDelete,
                    parameterBuilder.GetMarker("ShipperID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipperID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ShipperID
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateInsertCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Shipper.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.ShipperInsert,
                    parameterBuilder.GetMarker("CompanyName", 1),
                    parameterBuilder.GetMarker("Phone", 2)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CompanyName", 1),
                DbType.String,
                40,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CompanyName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Phone", 2),
                DbType.String,
                24,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Phone ?? (object)DBNull.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateUpdateCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Shipper.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.ShipperUpdate,
                    parameterBuilder.GetMarker("CompanyName", 1),
                    parameterBuilder.GetMarker("Phone", 2),
                    parameterBuilder.GetMarker("ShipperID", 3)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CompanyName", 1),
                DbType.String,
                40,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CompanyName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Phone", 2),
                DbType.String,
                24,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Phone ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipperID", 3),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ShipperID
            );

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected Salamanca.DataAccess.Data.ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private Salamanca.DataAccess.Data.ParameterBuilder _ParameterBuilder;

    }

}
